<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    :close-on-click-modal="false"
    @close="close"
    :append-to-body="true"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="0.8rem">
      <el-form-item label="单位内码" prop="code">
        <el-input v-model.trim="form.code" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="机构名称" prop="jigoumc">
        <el-input v-model.trim="form.jigoumc" autocomplete="off"></el-input>
      </el-form-item>
      <!-- <el-form-item label="机构类别">
        <el-select v-model="form.jigoulb" placeholder="请选择">
          <el-option
            v-for="(item, index) in jglblist"
            :key="index"
            :label="item.mingcheng"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="上级机构">
        <el-cascader
          :options="treeList"
          v-model="form.fid"
          :props="{
            checkStrictly: true,
            value: 'id',
            label: 'jigoumc',
            children: 'childList',
            emitPath: false,
          }"
          clearable
        ></el-cascader>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { add, update, getJGLBList } from '@/api/institution'

  export default {
    name: 'UserManagementEdit',
    props: {
      jglblist: {
        type: Array,
        default: function () {
          return []
        },
      },
      treeList: {
        type: Array,
        default: function () {
          return []
        },
      },
      currentkey: {
        default: null,
      },
    },
    data() {
      return {
        form: {
          fid: 0,
        },
        rules: {
          code: [
            { required: true, trigger: 'blur', message: '请输入单位内码' },
          ],
          jigoumc: [
            { required: true, trigger: 'blur', message: '请输入机构名称' },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    mounted() {
      let newtree = this.getTreeData(this.treeList)
      // eslint-disable-next-line vue/no-mutating-props
      this.treeList = newtree
    },
    methods: {
      // 递归判断列表，把最后的children设为undefined
      getTreeData(data) {
        for (var i = 0; i < data.length; i++) {
          if (data[i].childList.length == 0) {
            // children若为空数组，则将children设为undefined
            data[i].childList = undefined
          } else {
            // children若不为空数组，则继续 递归调用 本方法
            this.getTreeData(data[i].childList)
          }
        }
        return data
      },
      showEdit(row) {
        if (!row) {
          this.title = '新增'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
        }
        this.form.fid = this.currentkey
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (!this.form.id) {
              const { msg } = await add(this.form)
              this.$baseMessage(msg, 'success')
            } else {
              const { msg } = await update(this.form)
              this.$baseMessage(msg, 'success')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
<style lang="scss" scoped>
  .el-select--small {
    width: 100%;
  }
</style>
